﻿/*********************************************************************************
 * Copyright(c)  	2012 @kaleyroy
 * 创建日期：		2012-01-13 0:52:34
 * 文 件 名：		LevelService.cs 
 * CLR 版本: 		4.0.30319.239 
 * 创 建 者：		@kaleyroy 
 * 电子邮箱:		kaleyroy@gmail.com
**********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;

using USS.Domain;
using USS.Repository;
using USS.Repository.Specification;
using USS.Service;
using USS.Service.DataObject;
using USS.Service.ValueObject;

/// <summary>
/// 
/// 修改记录:
///	
/// </summary>
namespace USS.Service.Implement
{
    /// <summary>
    /// 级别服务对象
    /// </summary>
    public class LevelService : ILevelService
    {
        /// <summary>
        /// 级别数据访问对象
        /// </summary>
        public LevelRepository RepsLevel { get; set; }

        /// <summary>
        /// 新建级别信息
        /// </summary>
        /// <param name="levelData">级别数据对象</param>
        /// <returns>级别编号</returns>
        public int New(LevelData levelData)
        {
            int newID = (RepsLevel.FindAll().Max(l => (int?)l.LevelID) ?? 0) + 1;
            levelData.LevelID = newID;
            RepsLevel.Add(levelData.ToEntity());
            return levelData.LevelID;

        }

        /// <summary>
        /// 编辑级别信息
        /// </summary>
        /// <param name="levelData">级别数据对象</param>
        public void Edit(LevelData levelData)
        {
            RepsLevel.Update(levelData.ToEntity());         
        }

        /// <summary>
        /// 启用/禁用级别信息
        /// </summary>
        /// <param name="levelID">级别编号</param>
        /// <param name="flag">启用/禁用标识</param>
        public void Enable(int levelID, int flag)
        {
            Level level = RepsLevel.GetByKey(levelID);
            level.IsEnabled = flag;
            RepsLevel.Update(level);           
        }

        /// <summary>
        /// 删除级别
        /// </summary>
        /// <param name="levelID">级别标识</param>
        public void Delete(int levelID)
        {
            Level level = RepsLevel.GetByKey(levelID);
            RepsLevel.Remove(level);
        }

        /// <summary>
        /// 所有级别信息列表
        /// </summary>
        /// <param name="name">级别名称</param>
        /// <param name="page">当前页码</param>
        /// <param name="rows">页面大小</param>
        /// <param name="total">总记录数</param>
        /// <returns>级别列表集合</returns>
        public List<Level> AllLevels(string name, int page, int rows, out int total)
        {            
            var query = RepsLevel.FindAll(LevelSpecs.NameSpec(name));
            total = query.Count();
            return query.Skip((page - 1) * rows).Take(rows).ToList();
        }

        /// <summary>
        /// 有效级别信息列表
        /// </summary>
        /// <returns>级别列表集合</returns>
        public List<Level> EnabledLevels()
        {
            int flag = 1;
            var query = RepsLevel.FindAll(LevelSpecs.EnabledSpec(flag));
            return query.ToList();
        }
    }
}
